#include "Login.h"

Login::Login()
{

}
Login::~Login()
{
    m_addLoginTasks.clear();
    m_globalLoginTasks.clear();
}

void Login::AddSession(xTCPTask* task)
{
    std::lock_guard<std::mutex> guard(m_sessionMutex);
    m_addLoginTasks.push_back(task);
}

void Login::Update(uint32_t diff)
{
    {
        std::lock_guard<std::mutex> guard(m_sessionMutex);
        for(auto it=m_addLoginTasks.begin();it!=m_addLoginTasks.end();++it)
        {
            xTCPTask* task = *it;
            m_globalLoginTasks.push_back(task);
        }
        m_addLoginTasks.clear();
    }

    for(auto it=m_globalLoginTasks.begin();it!=m_globalLoginTasks.end();)
    {
        xTCPTask* task = *it;
        //task offline
        if(!task->Update(diff))
        {
            XLOG_INFO("TCP Session Offline,fd:[%d]",task->GetTCPSock()->GetFd());
            it = m_globalLoginTasks.erase(it);
            DELETE(task);
            continue;
        }
        ++it;
    }
}